package com.mini.mysql.repository;


;
import com.mini.mysql.model.BusinessStudentSecondOrderInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author: zhaocg
 * @created: 2020/09/25 16:15
 */
@Repository
public interface BusinessStudentSecondOrderInfoRepository extends JpaRepository<BusinessStudentSecondOrderInfo, Long>, JpaSpecificationExecutor<BusinessStudentSecondOrderInfo> {

    List<BusinessStudentSecondOrderInfo> findByPhoneNoAndDataStatesAndSecondTeacherIdIsNull(String studentPhone, Integer dataStates);

    BusinessStudentSecondOrderInfo findByBigOrderNoAndMiniOrderNo(String bigOrderNo, String miniOrderNo);

    @Query(value="select DISTINCT orderInfo.big_order_name from business_student_second_order_info orderInfo" , nativeQuery = true)
    List<String> findByOrderName();

    BusinessStudentSecondOrderInfo findFirstByPhoneNoAndMiniSkuCodeAndDataStatesOrderByAllocationTimeDesc(String phoneNo, String miniSkuCode, Integer dataStates);

    BusinessStudentSecondOrderInfo findFirstByPhoneNoAndMiniSkuCodeAndDataStatesOrderByCreateTime(String phoneNo, String miniSkuCode, Integer dataStates);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update business_student_second_order_info set chance_states=?1 , chance_reason =?2  where id=?3 ",nativeQuery = true )
    Integer updateChanceStates(Integer chanceStates, String chanceReason, Long id);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoOrderByCreateTimeDesc(String bigOrderNo);

    List<BusinessStudentSecondOrderInfo> findByPhoneNoAndMiniSkuCodeNotAndDataStatesAndMiniOrderNoIsNotNullOrderByCreateTimeDesc(String phoneNo, String miniSkuCode, Integer dataStates);

    List<BusinessStudentSecondOrderInfo> findByPhoneNoAndBigSkuCodeNotAndDataStatesAndMiniOrderNoIsNotNullOrderByCreateTimeDesc(String phoneNo, String bigSkuCode, Integer dataStates);

    List<BusinessStudentSecondOrderInfo> findByPhoneNoAndMiniSkuCodeNotAndDataStatesAndMiniOrderNoIsNullOrderByCreateTimeDesc(String phoneNo, String miniSkuCode, Integer dataStates);

    List<BusinessStudentSecondOrderInfo> findByPhoneNoAndBigSkuCodeNotAndDataStatesAndMiniOrderNoIsNullOrderByCreateTimeDesc(String phoneNo, String bigSkuCode, Integer dataStates);

    /**
     * 删除公海数据
     * @param bigOrderNo
     * @param miniOrderNo
     * @param dataStates
     * @return
     */
    Long deleteAllByBigOrderNoAndMiniOrderNoAndDataStates(String bigOrderNo, String miniOrderNo, Integer dataStates);

    Long deleteAllByBigOrderNoAndDataStates(String bigOrderNo, Integer dataStates);

    List<BusinessStudentSecondOrderInfo> findByBigOrderNoAndDataStatesAndChanceStatesAndAvailableOrderByCreateTimeDesc(String bigOrderNo, Integer dataStates, Integer chanceStates, Integer available);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoAndDataStatesAndChanceStatesAndAvailableOrderByCreateTimeDesc(String bigOrderNo, Integer dataStates, Integer chanceStates, Integer available);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoAndMiniOrderNoOrderByCreateTimeDesc(String bigOrderNo, String miniOrderNo);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoAndDataStatesAndChanceStatesOrderByCreateTimeDesc(String bigOrderNo, Integer dataStates, Integer chanceStates);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoAndMiniOrderNoAndDataStatesOrderByCreateTimeDesc(String bigOrderNo, String miniOrderNo, Integer dataStates);

    BusinessStudentSecondOrderInfo findTopByBigOrderNoAndMiniOrderNoAndDataStatesAndChanceStatesOrderByCreateTimeDesc(String bigOrderNo, String miniOrderNo, Integer dataStates, Integer chanceStates);

    List<BusinessStudentSecondOrderInfo> findByUpdateTimeBetweenOrderByUpdateTimeDesc(Date startTime, Date endTime);

    List<BusinessStudentSecondOrderInfo> findAllByIdIn(Set<Long> ids);
}
